package cn.iocoder.yudao.module.shop.service.agent;

import cn.iocoder.yudao.framework.common.pojo.PageParam;

import cn.iocoder.yudao.module.shop.dal.dataobject.agent.ManageDO;
import cn.iocoder.yudao.module.shop.dal.mysql.manage.ManageMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import cn.iocoder.yudao.module.shop.controller.admin.agent.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.agent.AgentDO;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.shop.dal.mysql.agent.AgentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

/**
 * 店铺代理商 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AgentServiceImpl implements AgentService {

    @Resource
    private AgentMapper agentMapper;
    @Resource
    private ManageMapper manageMapper;

    @Override
    public Long createAgent(AgentSaveReqVO createReqVO) {
        // 插入
        AgentDO agent = BeanUtils.toBean(createReqVO, AgentDO.class);
        agentMapper.insert(agent);
        // 返回
        return agent.getId();
    }

    @Override
    public void updateAgent(AgentSaveReqVO updateReqVO) {
        // 校验存在
        validateAgentExists(updateReqVO.getId());
        // 更新
        AgentDO updateObj = BeanUtils.toBean(updateReqVO, AgentDO.class);
        agentMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAgent(Long id) {
        // 校验存在
        validateAgentExists(id);
        // 删除
        agentMapper.deleteById(id);

        // 删除子表
        deleteManageByAgentId(id);
    }

    private void validateAgentExists(Long id) {
        if (agentMapper.selectById(id) == null) {
            throw exception(AGENT_NOT_EXISTS);
        }
    }

    @Override
    public AgentDO getAgent(Long id) {
        return agentMapper.selectById(id);
    }

    @Override
    public PageResult<AgentDO> getAgentPage(AgentPageReqVO pageReqVO) {
        return agentMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（店铺管理） ====================

    @Override
    public PageResult<ManageDO> getManagePage(PageParam pageReqVO, Long agentId) {
        return manageMapper.selectPage(pageReqVO,  agentId);
    }

    @Override
    public Long createManage(ManageDO manage) {
        manageMapper.insert(manage);
        return manage.getId();
    }

    @Override
    public void updateManage(ManageDO manage) {
        // 校验存在
        validateManageExists(manage.getId());
        // 更新
        manage.setUpdater(null).setUpdateTime(null); // 解决更新情况下：updateTime 不更新
        manageMapper.updateById(manage);
    }

    @Override
    public void deleteManage(Long id) {
        // 校验存在
        validateManageExists(id);
        // 删除
        manageMapper.deleteById(id);
    }

    @Override
    public ManageDO getManage(Long id) {
        return manageMapper.selectById(id);
    }

    private void validateManageExists(Long id) {
        if (manageMapper.selectById(id) == null) {
            throw exception(MANAGE_NOT_EXISTS);
        }
    }

    private void deleteManageByAgentId(Long agentId) {
        manageMapper.deleteById(agentId);
    }

}